package com.hm.plugin.httpclient.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSONObject;
import com.hm.plugin.httpclient.def.HttpClientDef;
import com.hm.plugin.httpclient.def.HttpMethods;

/**
 * @author shishun.wang
 * @date 上午11:20:04 2016年7月4日
 * @version 1.0
 * @describe
 */
@NotThreadSafe
public class MyHttpConfig {

	private HttpEntity httpEntity;

	private String uri;

	private HttpMethods httpMethod;

	private String userId;

	public MyHttpConfig currentUser(String userId) {
		this.userId = userId;
		return this;
	}

	public MyHttpConfig upload(String path) throws Exception {
		FileBody bin = new FileBody(new File(path));
		HttpEntity entity = MultipartEntityBuilder.create().addPart("files", bin).build();
		this.httpEntity = entity;
		return this;
	}

	public <T> MyHttpConfig json(T model) throws Exception {
		StringEntity entity = new StringEntity(JSONObject.toJSONString(model), HttpClientDef.Setting.Request.CHART);// 解决中文乱码问题
		{
			entity.setContentEncoding(HttpClientDef.Setting.Request.CHART);
			entity.setContentType("application/json");
		}
		this.httpEntity = entity;
		return this;
	}

	public MyHttpConfig map(Map<String, Object> param) throws Exception {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		{
			param.entrySet().forEach(requestParam -> {
				params.add(new BasicNameValuePair(requestParam.getKey(), requestParam.getValue() + ""));
			});
		}
		this.httpEntity = new UrlEncodedFormEntity(params);
		return this;
	}

	public MyHttpConfig method(HttpMethods httpMethod) {
		this.httpMethod = httpMethod;
		return this;
	}

	public MyHttpConfig uri(String uri) throws Exception {
		if (uri.contains("?") && uri.indexOf("?") < uri.indexOf("=")) {
			Map<String, Object> param = buildParas(uri.substring(uri.indexOf("?") + 1));
			this.map(param);
			uri = uri.substring(0, uri.indexOf("?"));
		}
		this.uri = uri;
		return this;
	}

	private Map<String, Object> buildParas(String paras) {
		String[] p = paras.split("&");
		String[][] ps = new String[p.length][2];
		int pos = 0;
		for (int i = 0; i < p.length; i++) {
			pos = p[i].indexOf("=");
			ps[i][0] = p[i].substring(0, pos);
			ps[i][1] = p[i].substring(pos + 1);
			pos = 0;
		}
		return buildParas(ps);
	}

	private Map<String, Object> buildParas(String[][] paras) {
		// 创建参数队列
		Map<String, Object> map = new HashMap<String, Object>();
		for (String[] para : paras) {
			map.put(para[0], para[1]);
		}
		return map;
	}

	public HttpEntity getHttpEntity() {
		return httpEntity;
	}

	public String getUri() {
		return uri;
	}

	public HttpMethods getHttpMethod() {
		return httpMethod;
	}

	public String getUserId() {
		return userId;
	}

}
